import Mock from 'mockjs'
import qs from 'qs'
// mock的配置
Mock.setup({
  // 随机延时500-1000毫秒 模拟网络延迟
  timeout: '500-1000'
})
// 拦截接口 /my/test
// 1.接口地址路径规则，需要匹配到它
// 2.请求方式
// 3.返回数据(函数中返回数据)
Mock.mock(/\/my\/test/, 'get', () => {
  const arr = []
  for (let i = 0; i < 5; i++) {
    // arr.push(Mock.mock('@id'))  模拟单条数据
    arr.push(Mock.mock({
      id: '@id',
      name: '@cname',
      image: '@image(160x160)'
    }))
  }

  return { msg: '获取数据成功', result: arr }
})
// 模拟我的收藏
// 对于后台接口不完整的情况，需要模拟哪些接口就保留哪些，完成的后台接口注释掉对应的模拟接口方法即可。
Mock.mock(/\/member\/collect/, 'get', config => {
  const queryString = config.url.split('?')[1]
  const queryObject = qs.parse(queryString)
  const items = []
  for (let i = 0; i < +queryObject.pageSize; i++) {
    items.push(Mock.mock({
      id: '@id',
      name: '@ctitle(10,20)',
      desc: '@ctitle(4,10)',
      price: '@float(100,200,2,2)',
      productionPlace: '@city',
      picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock('@integer(1,8)')}.jpg`
    }))
  }
  return {
    msg: '获取商品收藏成功',
    result: {
      counts: 30,
      pageSize: +queryObject.pageSize,
      page: +queryObject.page,
      items
    }
  }
})
// 模拟取消收藏
Mock.mock(/\/member\/collect\/batch/, 'delete', config => {
  const queryString = config.url.split('?')[1]
  const queryObject = qs.parse(queryString)
  const items = []
  for (let i = 0; i < +queryObject.pageSize; i++) {
    items.push(Mock.mock({
      id: '@id'
    }))
  }
  return {
    msg: '操作成功'
  }
})
// 模拟我的足迹
Mock.mock(/\/member\/browseHistory/, 'get', config => {
  const queryString = config.url.split('?')[1]
  const queryObject = qs.parse(queryString)
  const items = []
  for (let i = 0; i < +queryObject.pageSize; i++) {
    items.push(Mock.mock({
      id: '@id',
      name: '@ctitle(10,20)',
      desc: '@ctitle(4,10)',
      price: '@float(100,200,2,2)',
      browserTime: '@datetime()',
      picture: `http://zhoushugang.gitee.io/erabbit-client-pc-static/uploads/clothes_goods_${Mock.mock('@integer(1,8)')}.jpg`
    }))
  }
  return {
    msg: '获取我的足迹成功',
    result: {
      counts: 20,
      pageSize: +queryObject.pageSize,
      page: +queryObject.page,
      items
    }
  }
})
